from playwright.sync_api import sync_playwright
import pyperclip
import time


def run(prompt_text: str) -> None:
    with sync_playwright() as playwright:
        browser = playwright.chromium.launch(
            headless=False, 
            proxy={"server":"http://10260444:Cjb97403.@proxyhk.zte.com.cn:80"}
        )
        context = browser.new_context()
        page = context.new_page()
        
        # 登录
        page.goto("https://claudechn.com/pastel/#/login")
        page.get_by_placeholder("请输入用户名/邮箱").fill("153080686@qq.com")
        page.get_by_placeholder("请输入密码").fill("cjb000403")
        page.get_by_role("button", name="登录").click()
        
        # 等待登录完成
        page.wait_for_load_state("networkidle")

        # 选车并等待跳转
        page.locator(".brand").first.click()
        
        # 等待页面完全加载
        page.wait_for_load_state("load")
        page.wait_for_load_state("networkidle")
        time.sleep(2)  # 给页面充分时间加载
        
        try:
            # 检查当前URL是否包含chat
            if "chat" in page.url:
                # 点击新对话按钮
                page.get_by_role("link", name="New Chat").click()
            else:
                # 直接访问新对话页面
                page.goto("https://claudechn.com/new", wait_until="networkidle")
        except Exception as e:
            print(f"导航到新对话页面时出错: {str(e)}")
            # 尝试直接访问新对话页面
            page.goto("https://claudechn.com/new", wait_until="networkidle")
        
        # 确保页面加载完成
        page.wait_for_load_state("networkidle")
        time.sleep(1)
        
        # 输入提示词
        prompt_input = page.get_by_label("Write your prompt to Claude").locator("div")
        prompt_input.fill(prompt_text)
        
        # 发送消息
        page.get_by_label("Send Message").click()
        
        # 等待回复完成
        page.wait_for_load_state("networkidle")
        time.sleep(2)  # 额外等待以确保回复完全加载
        
        # 点击复制按钮
        page.get_by_role("button", name="Copy").click()
        
        # 获取剪贴板内容并打印
        response_text = pyperclip.paste()
        print("Claude的回复：")
        print(response_text)

        context.close()
        browser.close()

if __name__ == "__main__":
    # 设置要发送给 Claude 的提示词
    prompt = "你好，请介绍一下自己"
    run(prompt)
